package ii.oo.liu;

import org.junit.Test;

import java.io.*;
import java.lang.reflect.ParameterizedType;

/**
 * Created by Admin on 1/6 0006.
 */
public class TestRandomAccessFile {
    @Test
    public void test1(){
        RandomAccessFile inf = null;
        RandomAccessFile outf = null;
        try {
            inf = new RandomAccessFile("a.txt", "r");
            outf = new RandomAccessFile("b.txt", "rw");
            inf.seek(outf.getFilePointer());
            outf.seek(outf.getFilePointer());

            byte[] buf = new byte[1];
            int len;
            while ((len=inf.read(buf)) != -1) {
                outf.write(buf,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (outf != null) {
                try {
                    outf.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inf != null) {
                try {
                    inf.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }



    @Test
    public void test() throws Exception {
        RandomAccessFile inf = null;
        RandomAccessFile outf = null;
        try {
            inf = new RandomAccessFile("a.txt", "r");
            outf = new RandomAccessFile("b.txt", "rw");
            byte[] buf = new byte[1];
            while (inf.read(buf) != -1) {
                outf.write(buf);
                if (outf.length() == 5)
                break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (outf != null) {
                try {
                    outf.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inf != null) {
                try {
                    inf.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    class Method2 {
        private long position;

        public void go(String src, String desc) {
            RandomAccessFile inf = null;
            RandomAccessFile outf = null;
            try {
                inf = new RandomAccessFile(src, "r");
                outf = new RandomAccessFile(desc, "rw");
                byte[] buf = new byte[1];
                while (inf.read(buf) != -1) {
                    outf.write(buf);
                    if (outf.length() == 5)
                        position = 5;
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (outf != null) {
                    try {
                        outf.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (inf != null) {
                    try {
                        inf.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

















    class MethodShow {
        private int position=-1;
        public void show(String src, String desc)  {
            File inf=new File(src);
            File outf=new File(desc);
            FileInputStream in = null;
            FileOutputStream out = null;
            try {
                in = new FileInputStream(inf);
                out = new FileOutputStream(outf);

                byte[] buf = new byte[1];
                while (in.read(buf)!= -1) {
                    out.write(buf);
                    if (outf.length()== 5) {
                        position= 5;
                        throw new FileNotFoundException("你好!");
                    }
                }
            } catch (Exception e) {
                keep(src,desc,position);
            } finally {
                if(out!=null){
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(in!=null){
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }



        public void keep(String src, String desc,int position){
            try {
                RandomAccessFile in = new RandomAccessFile(src, "r");
                RandomAccessFile out = new RandomAccessFile(desc, "rw");
                in.seek(position);
                out.seek(position);
                byte[] buf = new byte[1];
                while (in.read(buf)!=-1){
                    out.write(buf);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}
